package com.egg.basic.config.pay;

import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * 阿里配置
 */
@Profile({"prod", "test", "dev"})
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "pay.ali")
public class PayAliConfig {

    // 应用编号
    private String appId;
    // 私钥
    private String privateKey;
    // 公钥
    private String publicKey;
    // 字符集 默认 UTF-8
    private String charset;
    // 加密方式 默认 RSA2
    private String signType;
    // 安全地址 默认 https://openapi.alipay.com/gateway.do
    private String serviceUrl;
    // 外网访问项目的域名，支付通知中会使用
    private String domain;
    // 支付回调地址
    private String payNotifyUrl;
    // 完整的支付回调地址
    private String fullPayNotifyUrl;

    @Bean
    public AliPayApiConfig initAliPayApiConfig() {
        AliPayApiConfig aliPayApiConfig;
        try {
            aliPayApiConfig = AliPayApiConfig.builder()
                    .setAppId(getAppId())
                    .setAliPayPublicKey(getPublicKey())
                    .setCharset(getCharset())
                    .setPrivateKey(getPrivateKey())
                    .setServiceUrl(getServiceUrl())
                    .setSignType(getSignType())
                    // 普通公钥方式
                    .build();
            // 证书模式
            //.buildByCert();
            AliPayApiConfigKit.putApiConfig(aliPayApiConfig);
            log.info("初始化成功 {}", AliPayApiConfig.class.getName());
            return aliPayApiConfig;
        } catch (Exception e) {
            log.error("初始化失败 {} 原因 {}", AliPayApiConfig.class.getName(), e.getMessage());
        }
        return null;
    }
}